<?php


namespace app\controller;

use app\BaseController;
use app\model\Images;
use app\model\Imgs;
use app\model\Imgsrelation;
use app\model\Sorts;
use app\model\Tags;
use app\model\Tagsrelation;
use think\facade\Db;
use think\facade\Route;
use think\facade\View;

class m extends BaseController
{
	
	//移动端首页
	public function index()
	{
		$ism = $this->isMobile();
		if ($ism) {
			//获取图片显示数据
			//id倒序查询4个数据
			$images = Images::order('id desc')->limit(4)->select();
			$imagesId = $images->column('id');
			
			$data = [];
			foreach ($imagesId as $i) {
				$data[] = [
					'id' => $i,
					'imgurl' => Imgs::where('imagesid', $i)->find()['imgurl'],
					'title' => Images::find($i)['title'],
				];
			}
			
			return view('m', [
				'data' => $data,
			]);
		} else {
			return Redirect('http://xs.com');
		}
		
		
	}
	
	//图片展示页
	public function img($id = '', $tag = '')
	{
		//查询title，分类，添加时间
		$data = Images::find($id);
		
		//查询二级分类名称
		$sortName2 = Tags::find($tag)['tagname'];
		if ($data == null) {
			return view('img', [
				'msg' => [
					'title' => '',
					'addtime' => ''
				],
				'list' => '',
				'sortname' => '',
				'sortcode' => '',
				'look' => '',
				'love' => '',
				'tags' => [],
				'sortname2' => '',
				'tagid' => $tag,
			]);
			
		} else {
			//查询分类
			//look自增
			Db::table('images')
				->where('id', $id)
				->inc('look')
				->update();
			
			$sort = Sorts::find($data['sortid']);
			// 查询到所有对应的imgurl
			$imgUrl = Db::name('imgs')->where('imagesid', $id)->paginate(1);
			
			//获取标签
			// $tagId = Tagsrelation::where('imagesid', $imagesId)->find()['tagid'];
			// $tagIdToArr = explode(',', $tagId);
			// $AllTagName = [];
			// foreach ($tagIdToArr as $i) {
			// 	$AllTagName[] = Tags::find($i)['tagname'];
			// }
			$tagId = Tagsrelation::where('imagesid', $id)->find()['tagid'];
			$tagIdToArr = explode(',', $tagId);
			$AllTagName = [];
			foreach ($tagIdToArr as $i) {
				$AllTagName[] = [
					'tagname' => Tags::find($i)['tagname'],
					'tagid' => $i,
				];
			}
			
			//二级分类
			
			
			// 渲染模板输出
			return view('img', [
				'list' => $imgUrl,
				'msg' => $data,
				'sortname' => $sort['sortname'],
				'sortcode' => $sort['sortcode'],
				'title' => $data['title'],
				'look' => $data['look'],
				'love' => $data['love'],
				'tags' => $AllTagName,
				'sortname2' => $sortName2,
				'tagid' => $tag,
			]);
		}
	}
	
	//标签显示页面
	public function tag($id = '')
	{
		$tagId = $id;
		$sqlTags = Tags::find($id);
		$tagName = $sqlTags['tagname'];
		$sortId = $sqlTags['sortid'];
		$sortName = Sorts::find($sortId)['sortname'];
		
		$imagesId = Db::name('tagsrelation')
			->whereLike('tagid', "%$id%")
			->order('id desc')
			// ->limit(4)
			->select()
			->column('imagesid');
		
		// dump($imagesId);
		
		// $images = Images::order('id desc')->limit(4)->select();
		// $imagesId = $images->column('id');
		
		$data = [];
		foreach ($imagesId as $i) {
			$data[] = [
				'id' => $i,
				'imgurl' => Imgs::where('imagesid', $i)->find()['imgurl'],
				'title' => Images::find($i)['title'],
				'tagid' => $tagId,
			];
		}
		
		return view('tag', [
			'data' => $data,
			'sortname' => $sortName,
			'tagname' => $tagName,
		]);
		
	}
	
	//二级分类页导航
	public function sort()
	{
		$allSorts = Sorts::select();
		$allSortsName = $allSorts->column('sortname');
		$allSortsId = $allSorts->column('id');
		// dump($allSortsName);
		// dump($allSortsId);
		$data = [];
		
		foreach ($allSortsId as $i) {
			
			$data[] = [
				
				'sortname' => Sorts::find($i)['sortname'],
				'allsort2'=>Tags::where('sortid',$i)->select()->toArray(),
				// 'allsort2id'=>Tags::where('sortid',$i)->select()->column('id'),
			
			
			];
		}
		
		$q = Tags::select();
		// dump($q->toArray());
		
		dump($data);
		// dump($data);
		return view('sort', [
			'data' => $data,
		
		]);
	}
	
	public function meinv()
	{
		return view('meinv');
	}


//
	public function isMobile()
	{
		// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
		if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
			return true;
		}
		// 如果via信息含有wap则一定是移动设备
		if (isset ($_SERVER['HTTP_VIA'])) {
			// 找不到为flase,否则为true
			return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
		}
		// 脑残法，判断手机发送的客户端标志,兼容性有待提高
		if (isset ($_SERVER['HTTP_USER_AGENT'])) {
			$clientkeywords = array('nokia',
				'sony',
				'ericsson',
				'mot',
				'samsung',
				'htc',
				'sgh',
				'lg',
				'sharp',
				'sie-',
				'philips',
				'panasonic',
				'alcatel',
				'lenovo',
				'iphone',
				'ipod',
				'blackberry',
				'meizu',
				'android',
				'netfront',
				'symbian',
				'ucweb',
				'windowsce',
				'palm',
				'operamini',
				'operamobi',
				'openwave',
				'nexusone',
				'cldc',
				'midp',
				'wap',
				'mobile'
			);
			// 从HTTP_USER_AGENT中查找手机浏览器的关键字
			if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
				return true;
			}
		}
		// 协议法，因为有可能不准确，放到最后判断
		if (isset ($_SERVER['HTTP_ACCEPT'])) {
			// 如果只支持wml并且不支持html那一定是移动设备
			// 如果支持wml和html但是wml在html之前则是移动设备
			if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
				return true;
			}
		}
		return false;
	}
}